# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Fabian Bornschein <fabiscafe-cat-mailbox-dog-org>

pkgname=gnome-remote-desktop
pkgver=44.1
pkgrel=1
pkgdesc="GNOME Remote Desktop server"
url="https://wiki.gnome.org/Projects/Mutter/RemoteDesktop"
arch=(x86_64)
license=(GPL2)
depends=(
  cairo
  dconf
  freerdp
  fuse3
  glib2
  libdrm
  libepoxy
  libfdk-aac
  libnotify
  libsecret
  libvncserver
  libxkbcommon
  pipewire
  systemd
  tpm2-tss
)
makedepends=(
  asciidoc
  ffnvcodec-headers
  git
  meson
)
checkdepends=(
  mutter
  python-dbus
  python-gobject
  wireplumber
)
groups=(gnome)
_commit=66e0fa21c87f82d7aaf71846dc408fb832d43146  # tags/44.1^0
source=("git+https://gitlab.gnome.org/GNOME/gnome-remote-desktop.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd $pkgname
}

build() {
  local meson_options=(
    -D vnc=true
  )

  arch-meson $pkgname build "${meson_options[@]}"
  meson compile -C build
}

_check() (
  export XDG_CONFIG_HOME="$PWD/config-dir" XDG_RUNTIME_DIR="$PWD/runtime-dir"
  mkdir -p -m 700 "$XDG_CONFIG_HOME" "$XDG_RUNTIME_DIR"

  export GSETTINGS_SCHEMA_DIR="$PWD/build/src" GSETTINGS_BACKEND=keyfile
  glib-compile-schemas "$GSETTINGS_SCHEMA_DIR"

  openssl req -new -newkey rsa:4096 -days 720 -nodes -x509 \
    -subj /C=DE/ST=NONE/L=NONE/O=GNOME/CN=gnome.org \
    -keyout tls.key -out tls.crt

  gsettings set org.gnome.desktop.remote-desktop.rdp tls-cert "$PWD/tls.crt"
  gsettings set org.gnome.desktop.remote-desktop.rdp tls-key "$PWD/tls.key"
  gsettings set org.gnome.desktop.remote-desktop.rdp screen-share-mode extend
  gsettings set org.gnome.desktop.remote-desktop.rdp enable true
  gsettings set org.gnome.desktop.remote-desktop.vnc enable true

  pipewire &
  _p1=$!

  wireplumber &
  _p2=$!

  trap "kill $_p1 $_p2; wait" EXIT

  export TCTI=tabrmd:bus_type=session

  meson test -C build --print-errorlogs -t 3
)

check() {
  dbus-run-session bash -c "$(declare -f _check); _check"
}

package() {
  meson install -C build --destdir "$pkgdir"
}

# vim:set sw=2 sts=-1 et:
